package com.dycn.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


@Controller
@RequestMapping("pic")
public class PicCOntroller {

    @ResponseBody
    @RequestMapping( path = "upload")
    public  Map<String, Object> upload(HttpServletRequest request) {
        Map<String, Object> json = new HashMap<String, Object>();
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyMMddHHmmss");
        /** 页面控件的文件流* */
        MultipartFile multipartFile = null;
        Map map =multipartRequest.getFileMap();
        int radom= (int) (Math.random()*1000);
        String fileName=simpleDateFormat.format(new Date())+radom;
        for (Iterator i = map.keySet().iterator(); i.hasNext();) {
            Object obj = i.next();
            multipartFile=(MultipartFile) map.get(obj);
            String oName=multipartFile.getOriginalFilename();
            String names[]=multipartFile.getOriginalFilename().split("\\.");
            fileName+="."+names[1];
            //int radom= (int) (Math.random()*1000);
            //String fileName=simpleDateFormat.format(new Date())+radom;
            //String upToken = FileUtils.getSign("kuashitong");
            try {
                // 第二种：获取项目路径    D:\git\daotie\daotie
                File directory = new File("");// 参数为空
                String filePath = request.getSession().getServletContext().getRealPath("/") + "upload";

                //String courseFile =  this.getClass().getClassLoader().getResource("").getPath()+"/src/resources/static";
                File fileDic=new File(filePath);
                if(!fileDic.exists()){
                    fileDic.mkdirs();
                }
                File file=new File(fileDic,fileName);
                if(!file.exists()){
                    file.createNewFile();
                }
                ByteArrayInputStream fis=new ByteArrayInputStream(multipartFile.getBytes());
                FileOutputStream fos=new FileOutputStream(file);
                int ch;
                byte[] buffer = new byte[1024];
                while((ch = fis.read(buffer) ) != -1){
                    fos.write(buffer,0,ch);
                }
                fis.close();
                fos.flush();
                fos.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
        //String filename=simpleDateFormat.format(new Date())+(int)(Math.random()*10000);
        json.put("code", "200");
        json.put("src","/upload/"+fileName);
        json.put("name",fileName);
        return json ;
    }
}
